#  验证通过----诗词ai
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from flask import Flask, request
import json

text_generation_zh = pipeline(Tasks.text_generation, model='damo/nlp_gpt3_poetry-generation_chinese-large')

app = Flask(__name__)

# 处理文本类
class TextProcessor:
    def __init__(self, text):
        self.text = text

    # 业务处理方法
    def reverse_text(self):
        result_zh = text_generation_zh(self.text)
        result_zh = result_zh['text']
        return result_zh

@app.route('/ai', methods=['POST'])
def ai():
    # 获取请求数据
    data = request.get_data()
    param0 = data.decode('utf-8')
    print("请求参数：",param0)

    # 创建一个文本处理器
    processor = TextProcessor(param0)

    # 调用业务处理方法
    result_zh = processor.reverse_text()
    print("处理结果:",result_zh)

    # 构造响应数据
    response_data = {
        'result': result_zh
    }
    response = json.dumps(response_data, ensure_ascii=False).encode('utf-8')

    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8888)

